/**
 * 
 */
package com.yinhai.activity;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.lz.util.Utils;
import com.yinhai.adapter.MoreAppAdListAdapter;
import com.yinhai.cfg.LoginConfig;
import com.yinhai.cfg.LoginConfigUtil;
import com.yinhai.constant.UrlConstant;
import com.yinhai.model.NewsContent;
import com.yinhai.service.NoticeNewsServiceImpl;
import com.yinhai.util.IsInstallApp;

/**
 * @author Juyon
 * 
 */
public class AppFragment extends Fragment {
    
    LinearLayout li, laydx, laybj, laycq, laybgs,layhjy;
    
    AQuery aq = null;
    
    Context ct = null;
    
    public String phone;
    
    MoreAppAdListAdapter listadpter;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_app, null);
        aq = new AQuery(getActivity(), getView());
        ct = getActivity();
        LoginConfig log = LoginConfigUtil.getConfig(ct);
        phone = log.getUsername();
        
        TextView tt_jtgg = (TextView)view.findViewById(R.id.tt_jtgg);
        tt_jtgg.setFocusable(true);
        tt_jtgg.setFocusableInTouchMode(true);
        tt_jtgg.requestFocus();
        
        // findNotices();
        //和教育
        if (IsInstallApp.isAvilible(ct, "cn.qtone.xxt.guangdong")) {
            TextView vs = (TextView)view.findViewById(R.id.hjy_txt);
            vs.setText("打开");
        }
        //重庆城
        if (IsInstallApp.isAvilible(ct, "com.cmcc.wificity")) {
            TextView vs = (TextView)view.findViewById(R.id.cqc_txt);
            vs.setText("打开");
        }
        //掌上厅
        if (IsInstallApp.isAvilible(ct, "com.greenpoint.android.mc10086.activity")) {
            TextView vs = (TextView)view.findViewById(R.id.moa_txt);
            vs.setText("打开");
        }
        
        NoticeNewsServiceImpl noticedao = new NoticeNewsServiceImpl(ct);
        List<NewsContent> list = noticedao.findByList();
        
        if (list != null && list.size() > 0) {
            aq.id(R.id.tvcontent).text(list.get(0).getTitle());
            aq.id(R.id.img).image(list.get(0).getImg(), Utils.getImageOptions(ct, R.drawable.main,0));
            String ids = "";
            for (int i = 0; i < list.size(); i++) {
                ids = ids + list.get(i).getUuid() + ",";
            }
            String idss = ids.substring(0, ids.length() - 1);
            finddelNotices(idss);
        } else {
            aq.id(R.id.img).image(R.drawable.main);
        }
        
        // findNotices();
        li = (LinearLayout)view.findViewById(R.id.news_content);
        li.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(getActivity(), NewsContentActivity.class);
                startActivity(it);
            }
        });
        
        laydx = (LinearLayout)view.findViewById(R.id.laydx);
        laydx.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(getActivity(), MsgQFActivity.class);
                startActivity(it);
            }
        });
        laybj = (LinearLayout)view.findViewById(R.id.laybj);
        laybj.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(getActivity(), CloudNoteActivity.class);
                startActivity(it);
            }
        });
        
        laycq = (LinearLayout)view.findViewById(R.id.laycq);
        laycq.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                PackageManager packageManager = getActivity().getPackageManager();
                
                if (!IsInstallApp.isAvilible(ct, "com.cmcc.wificity")) {
                    Intent it = new Intent(getActivity(), NewsContentCQCActivity.class);
                    it.putExtra("ISCQC", "2");
                    startActivity(it);
                } else {
                    Intent intent = new Intent();
                    intent = packageManager.getLaunchIntentForPackage("com.cmcc.wificity");
                    startActivity(intent);
                }
                
            }
        });
        
        laybgs = (LinearLayout)view.findViewById(R.id.laybgs);
        laybgs.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                PackageManager packageManager = getActivity().getPackageManager();
                
                if (!IsInstallApp.isAvilible(ct, "com.greenpoint.android.mc10086.activity")) {
                    Intent it = new Intent(getActivity(), NewsContentCQCActivity.class);
                    it.putExtra("ISCQC", "3");
                    startActivity(it);
                } else {
                    Intent intent = new Intent();
                    intent = packageManager.getLaunchIntentForPackage("com.greenpoint.android.mc10086.activity");
                    startActivity(intent);
                }
            }
        });
        
        
        layhjy = (LinearLayout)view.findViewById(R.id.layhjy);
        layhjy.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                PackageManager packageManager = getActivity().getPackageManager();
                
                if (!IsInstallApp.isAvilible(ct, "cn.qtone.xxt.guangdong")) {
                    Intent it = new Intent(getActivity(), NewsContentCQCActivity.class);
                    it.putExtra("ISCQC", "1");
                    startActivity(it);
                } else {
                    Intent intent = new Intent();
                    intent = packageManager.getLaunchIntentForPackage("cn.qtone.xxt.guangdong");
                    startActivity(intent);
                }
            }
        });
        
        return view;
    }
    
    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        NoticeNewsServiceImpl noticedao = new NoticeNewsServiceImpl(ct);
        
        List<NewsContent> list = noticedao.findByList();
        
        if (list != null && list.size() > 0) {
            aq.id(R.id.tvcontent).text(list.get(0).getTitle());
            aq.id(R.id.img).image(list.get(0).getImg(), true, true);
            String ids = "";
            for (int i = 0; i < list.size(); i++) {
                ids = ids + list.get(i).getUuid() + ",";
            }
            String idss = ids.substring(0, ids.length() - 1);
            finddelNotices(idss);
            
        } else {
            aq.id(R.id.img).image(R.drawable.main);
        }
        super.onResume();
    }
    
    // 删除已经删除的本机公告
    private void finddelNotices(String ids) {
        LoginConfig log = LoginConfigUtil.getConfig(ct);
        String phone = log.getUsername();
        String url = UrlConstant.LOCAL_URI + "findDelNotices";
        url = url + "?phone=" + phone + "&ids=" + ids;
        aq.progress(0).ajax(url, JSONObject.class, new AjaxCallback<JSONObject>() {
            
            public void callback(String url, JSONObject json, AjaxStatus status) {
                // TODO Auto-generated method stub
                if (json != null) {
                    try {
                        JSONArray js = json.getJSONArray("data");
                        NoticeNewsServiceImpl noticedao = new NoticeNewsServiceImpl(ct);
                        for (int i = 0; i < js.length(); i++) {
                            noticedao.delete(js.getString(i));
                        }
                    } catch (Exception e) {
                        
                    }
                } else {
                    
                }
            }
        });
    }
    
}
